$(function() {
    /* 全选 */
    $('.checkall').click(function() {
        $('.checkitem').attr('checked', this.checked)
    });

    /* 批量操作按钮 */
    if ($('#batchAction').length == 1) {
        $('.batchButton').click(function() {
            /* 是否有选择 */
            if ($('.checkitem:checked').length == 0) { //没有选择
                return false;
            }
            /* 运行presubmit */
            if ($(this).attr('presubmit')) {
                if (!eval($(this).attr('presubmit'))) {
                    return false;
                }
            }
            /* 获取选中的项 */
            var items = '';
            $('.checkitem:checked').each(function() {
                items += this.value + ',';
            });
            items = items.substr(0, (items.length - 1));
            /* 将选中的项通过GET方式提交给指定的URI */
            var uri = $(this).attr('uri');
            window.location = uri + '&' + $(this).attr('name') + '=' + items;
        });
    }

    /* 缩小大图片 */
    $('.makesmall').each(function() {
        if (this.complete) {
            makesmall(this, $(this).attr('max_width'), $(this).attr('max_height'));
        } else {
            $(this).load(function() {
                makesmall(this, $(this).attr('max_width'), $(this).attr('max_height'));
            });
        }
    });
});

function drop_confirm(msg, url) {
    if (confirm(msg)) {
        if (url == undefined) {
            return true;
        }
        window.location = url;
    } else {
        if (url == undefined) {
            return false;
        }
    }
}

function makesmall(obj, w, h) {
    srcImage = obj;
    var srcW = srcImage.width;
    var srcH = srcImage.height;
    if (srcW == 0) {
        obj.src = srcImage.src;
        srcImage.src = obj.src;
        var srcW = srcImage.width;
        var srcH = srcImage.height;
    }
    if (srcH == 0) {
        obj.src = srcImage.src;
        srcImage.src = obj.src;
        var srcW = srcImage.width;
        var srcH = srcImage.height;
    }

    if (srcW > srcH) {
        if (srcW > w) {
            obj.width = newW = w;
            obj.height = newH = (w / srcW) * srcH;
        } else {
            obj.width = newW = srcW;
            obj.height = newH = srcH;
        }
    } else {
        if (srcH > h) {
            obj.height = newH = h;
            obj.width = newW = (h / srcH) * srcW;
        } else {
            obj.width = newW = srcW;
            obj.height = newH = srcH;
        }
    }
    if (newW > w) {
        obj.width = w;
        obj.height = newH * (w / newW);
    } else if (newH > h) {
        obj.height = h;
        obj.width = newW * (h / newH);
    }
}

admin = {
    swloverlay: function() {
        var o = $('<div class="wloverlay" id="wloverlay"></div>');
        $('body').append(o);
        $('#wloverlay').get(0).oncontextmenu = function() {
            return false;
        };
    },
    hwloverlay: function() {
        $('body > .wloverlay').remove();
    },
    showPop: function(options) {
        var content = $(options.id);
        var con_width = content.outerWidth();
        var con_height = content.outerHeight();
        var win_width = jQuery(options.maxWrap).width();
        var win_height = jQuery(options.maxWrap).height();
        this.swloverlay();
        content.css({
            'left': (win_width - con_width) / 2 + 'px',
            'top': (win_height - con_height) / 2 + 'px'
        }).show();
    },
    hidePop: function(options) {
        jQuery(options.id).hide();
        this.hwloverlay();
    },
    sConfirm: function(settings) {
        var obj = $('<div id="wlconfirmPop"><ul class="wlconfirmPop-t f-cb"><li class="wcp-l">提示</li><li class="wcp-r"><a id="wlconfirmPop-close">×</a></li></ul><p><span>' + settings.info + '</span></p><ul class="wlconfirmPop-btn f-cb"><li><a class="b-radius" id="wlconfirmPop-cancel">取消</a></li><li><a class="b-radius" id="wlconfirmPop-yes">确定</a></li></ul></div>');
        $('body').append(obj);
        var T = this;
        var d = {
            o: $('#wlconfirmPop'),
            b: $('#wlconfirmPop-yes'),
            j: $('#wlconfirmPop-cancel'),
            k: $('#wlconfirmPop-close'),
            ow: function() {
                return this.o.outerWidth(true);
            },
            oh: function() {
                return this.o.outerHeight(true);
            },
            w: $(window).width(),
            h: $(window).height()
        };
        d.o.css({
            'left': (d.w - d.ow()) / 2 + 'px',
            'top': (d.h - d.oh()) / 2 + 'px'
        }).show();
        this.swloverlay();
        if (settings.yes) {
            d.b.hide();
        } else {
            d.b.bind('click', function() {
                if (settings.success) {
                    settings.success();
                }
                T.hConfirm();
                T.hwloverlay();
            });
        }
        if (settings.cancel) {
            d.j.hide();
        } else {
            d.j.bind('click', function() {
                if (settings.fail) {
                    settings.fail();
                }
                T.hConfirm();
                T.hwloverlay();
            });
        }
        d.k.bind('click', function() {
            T.hConfirm();
            T.hwloverlay();
        });
    },
    hConfirm: function() {
        $('body > #wlconfirmPop').remove();
    }
}